package AdminClient;
import static AdminClient.Debug.*;
import AdminClient.*;

public class Player {
	private String name;
	private String room;
	private Msg_Pipe update_pipe;
	private String password;
	private boolean active;
	private static final String delimiter = Msg_Pipe.delimiter;

	public Player(String n, String r, String p) {
		name = n;
		room = r;
		password = p; 
		active = false;
	}

	public String get_name() {
		return(name);
	}
	
	public void set_name(String name)
	{
		this.name=name;
	}

	public String get_room() {
		return(room);
	}

	public String get_password() {
		return(password);
	}

	public void set_room(String room) {
		this.room = room;
	}

	public void set_update_pipe(Msg_Pipe up) {
		update_pipe = up;
	}

	public void set_active(boolean active) {
		this.active = active;
	}

	public boolean get_active() {
		return(active);
	}
	
	public boolean pick_up_item(Item i)
	{
		i.pick_up();
		i.set_location(name);
		return true;
	}
	public boolean drop_item(Item i)
	{
		i.put_down();
		i.set_location(room);
		return true;
	}
	public String toString() {
		return(name + delimiter + room + delimiter + password + delimiter + active);
	}

	public boolean update_notify(String message) {
		boolean retval = false;

		if (update_pipe != null)
			retval = update_pipe.send_message(message);

		return(retval);
	}

	public static Player fromString(String str) throws Exception {
		String[] res = str.split("\\" + delimiter);
		if (res.length != 3) // 1 + number of attributes in class
			throw (new InvalidMessageException("invalid Player"));
		return(new Player(res[0], res[1], res[2]));
	}

}